package com.machine.filling.function.init.OilGunList;

import android.content.Context;

import com.google.gson.Gson;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.db.modle.GunInfoModle;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.http.response.GetTokenResBean;
import com.machine.filling.net.http.response.OilGunListResBean;
import com.machine.filling.util.CustomToast;
import com.machine.filling.util.SpUtils;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/14 0014.
 * 添加油枪界面
 */

public class OilGunListPresenter implements OilGunListContract.Presenter {
    private Context mContext;
    private OilGunListContract.View mView;

    public OilGunListPresenter(Context mContext, OilGunListContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
    }

    @Override
    public void start() {

    }

    @Override
    public void distoryContext() {


    }

    /**
     * 获取已添加的油枪 如果数据库丢失，会出现问题：得不到machineId
     */
    @Override
    public void getOilGunList() {
        String machineId = mView.getMationId();
        mView.showProgress(null);
        APIHttpManager.getInstance().getOilGunList(machineId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilGunListResBean bean = new Gson().fromJson(result, OilGunListResBean.class);
                for (OilGunListResBean.ResultBean.GunsBean resBean : bean.getResult().getGuns()) {
                    GunInfoModle gun = new GunInfoModle();
                    gun.setGunNum(resBean.getNum());
                    gun.setTypeId(resBean.getTypeId());
                    gun.setCreateD(resBean.getTypeId());
                    gun.setStationId(resBean.getSellerId());
                    gun.setGunName(resBean.getName());
                    gun.setGunId(resBean.getId());
                    gun.setInitAmount(resBean.getInitAmount());
                    gun.setMachineId(resBean.getMachineId());
                    gun.setPortNum(resBean.getPortNum());
                    gun.setPotId(resBean.getPotId());
                    gun.setPotNum(resBean.getOilPotNum());
                    gun.setTypeName(resBean.getTypeName());
                    gun.setTypeShortName(resBean.getTypeName());
                    GunInfoDao.getInstance().addGun(gun);
                }
                mView.showOilGunListResult(bean.getResult().getGuns());

            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 完成初始化
     */
    @Override
    public void commitInitInfo() {

        String sellerId = mView.getStationId();
        String machineId = mView.getMationId();
        String machineNum = mView.getMachineNum();
        String deviceId = mView.getClientId();
        mView.showProgress(null);
        APIHttpManager.getInstance().getToken(sellerId, machineId, machineNum, deviceId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                GetTokenResBean bean = new Gson().fromJson(result, GetTokenResBean.class);
                if (bean.getResult() != null) {
                    mView.showCommitInitSuccessResult(bean);
                } else {
                    CustomToast.showToast(mContext, bean.getMsg());
                }

            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 删除油枪
     *
     * @param oilGunId
     */
    @Override
    public void deleteOilGun(final int position, final String oilGunId) {
        // TODO: 2018/2/16 0016 删除油枪
        APIHttpManager.getInstance().delGun(oilGunId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                mView.showDeleteSuccess(position);
                GunInfoDao.getInstance().deletetByGunId(oilGunId);
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorCode);
            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 系统还原
     */
    @Override
    public void reSetClient() {
        String stationId = mView.getStationId();
        String machineId = mView.getMationId();
        String clientId = mView.getClientId();
        mView.showProgress(null);
        APIHttpManager.getInstance().reSetOilClient(
//                "21","11","22",
                stationId, machineId, clientId,
                new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                ClientInfoDao.getInstance().deleteAll();
                GunInfoDao.getInstance().deleteAll();
                SpUtils.getInstance(mContext).setInitComplete(false);
                mView.showReSetSuccessResult();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

}
